﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WBGDal
{
    public class UtilBanco
    {
        public static void ExecSemRetorno(String comando)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            try
            {
                conn = Conexao.Get();
                cmd = new SqlCommand(comando, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.InnerException.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw ex;
            }
            finally
            {
                if (conn != null)
                    conn.Dispose();
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        public static DataTable ExecComRetorno(String comando)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataAdapter da = null;
            DataTable dt = null;
            try
            {
                conn = Conexao.Get();
                cmd = new SqlCommand(comando, conn);
                da = new SqlDataAdapter(cmd);
                dt = new DataTable();
                da.Fill(dt);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message,"Erro",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            finally
            {
                if (conn != null)
                    conn.Dispose();
                if (cmd != null)
                    cmd.Dispose();
                if (da != null)
                    da.Dispose();
                if (dt != null)
                    dt.Dispose();
            }
            return dt;
        }

        public static string ValorOuNulo(Nullable<DateTime> nullableValue)
        {
            if (nullableValue.HasValue)
                return "'"+ nullableValue.Value.ToString() +"'";
            else
                return "null";
        }
    }
}
